// pages/show/index.js
import api from '../../api/index'
import { AppId } from '../../api/config'
import { userInfoStore } from '../../store/userInfo'
var WXBizDataCrypt = require('../../utils/RdWXBizDataCrypt.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userLevel: 0,
    banners: [],
    result:{},
    dataList: [],
    uploading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.setData(
      {
        userLevel: userInfoStore.userInfo.level
      });
    var self = this;
    api.getbanner({ pageNo: 4 }).then(res => {
      self.setData({
        banners: res.result
      })
    })

  },

  onShow: function(){
    var self = this;
    this.setData({ dataList: [], uploading: false }, () => {
      self.getList(1);
    })
    let giftBag = this.selectComponent("#giftbag");
    app.displayGiftBag(giftBag);
  },

  getList: function (pageIndex){
    var self=this;
    var param = {
      openId: userInfoStore.userInfo.openId,
      pageNo: pageIndex
    }
    if (app.globalData.inviteCode)
      param = Object.assign(param, { recomendCode: app.globalData.inviteCode })
    api.getliveplaylist(param).then(res=>{
      console.log(res);
      let {result} = res;
      let {records} = result;
      if (self.data.uploading){
        self.data.dataList.push(...records);
        self.setData({
          dataList: self.data.dataList,
          result: result
        })
      }else{
        self.setData({
          dataList: records,
          result: result
        })
      }
      console.log("complete");
      self.setData({ uploading: false });
      wx.stopPullDownRefresh();
    }, err => { 
      console.log("err");
      self.setData({ uploading: false });
      wx.stopPullDownRefresh();
    });
  },


  getPhoneNumber: function (e) {
    console.log(e);
    if (e.detail.iv) {
      let { session_key } = userInfoStore.userInfo;
      let pc = new WXBizDataCrypt(AppId, session_key);
      var ret = pc.decryptData(e.detail.encryptedData, e.detail.iv)
      console.log('解密后 data: ', ret)

      var param = {
        openId: userInfoStore.userInfo.openId,
        phone: ret.phoneNumber,
        level: 1
      }
      if (app.globalData.inviteCode)
        param = Object.assign(param, { recomendCode: app.globalData.inviteCode })
      var self = this
      var quanurl = e.currentTarget.dataset.src;
      api.saveUser(param).then(res => {
        let { userInfo } = userInfoStore;
        let { result } = res;
        result.session_key = userInfo.session_key;
        userInfoStore.updateUserInfo(result);
        wx.showModal({
          title: "提交成功",
          showCancel: false,
          confirmText: "我知道了",
          success: function (res) {
            if (res.confirm) {
              wx.previewImage({
                current: quanurl,
                urls: [quanurl]
              });
            }
          }
        })
      }, err => {
        wx.showModal({
          title: "提交失败",
          content: err,
          showCancel: false,
          confirmText: "我知道了"
        })
      })

    }
  },

  showcode:function(e){
    let url = e.currentTarget.dataset.src;
    wx.previewImage({
      current: url,
      urls: [url]
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var self=this;
    this.setData({ dataList: [], uploading:false},()=>{
      self.getList(1);
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.uploading) {
      var self=this;
      this.setData({ uploading: true },()=>{
        
        var current = parseInt(self.data.result.current);
        var pages = self.data.result.pages;
        if (current < pages) {
          self.getList(current + 1);
        }

      });
      
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    getApp().share();
  }
})